package com.konsung.ksdc.manager.community;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.RegisterUserNetBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 创建者     kgh
 * 创建时间   2016/5/27 15:28
 * 描述  联系人适配器
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class ContactListAdapter extends BaseAdapter {
    private List<RegisterUserNetBean> contacts = new ArrayList<RegisterUserNetBean>();

    /**
     * 构造方法
     * @param contacts 数据
     */
    public ContactListAdapter(List<RegisterUserNetBean> contacts) {
        this.contacts.clear();
        this.contacts.addAll(contacts);
    }

    @Override
    public int getCount() {
        return this.contacts.size();
    }

    @Override
    public Object getItem(int i) {
        return this.contacts.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        RegisterUserNetBean user = this.contacts.get(position);
        String picPath = StringUtils.appendPicPath(user.getUserPic());

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(MyApplication.getContext())
                    .inflate(R.layout.item_listview_contacts, null);
            ButterKnife.bind(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Picasso.with(container.getContext())
                .load(picPath)
                .placeholder(R.drawable.pic_talkbox_sel)
                .error(R.drawable.pic_talkbox_sel)
                .into(holder.imgContactPic);
        holder.txtNickName.setText(user.getNickName());
        holder.txtContactNote.setText(user.getUserNote());
        holder.userId = user.getUserId();

        return convertView;
    }

    /**
     * 设置联系人
     *
     * @param contacts 数据
     */
    public void setContacts(List<RegisterUserNetBean> contacts) {
        this.contacts.clear();
        this.contacts.addAll(contacts);
    }

    /**
     * 内部类
     */
    public static class ViewHolder {
        public String userId;
        @Bind(R.id.img_contact_pic)
        public ImageView imgContactPic;
        @Bind(R.id.txt_nick_name)
        public TextView txtNickName;
        @Bind(R.id.txt_contact_note)
        public TextView txtContactNote;

    }
}
